---
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r, results='asis', echo=FALSE, message=FALSE}
rm(list=ls())
library(dplyr)
library(stringr)
library(stargazer)
library(knitr)
load("../1_Data/slr_cleaned_2022.RData")
slr_census_final<-slr_cleaned

#STEP 1 - get an idea of matching levels
#######################################################################
a<-as.data.frame(table(slr_census_final$state,slr_census_final$slr_census_mergestatus))
a1<-subset(a, Var2=="unmatched")
a2<-subset(a, Var2=="fuzzy matched")
a3<-subset(a, Var2=="exact matched")

a1$n1<-a1$Freq #unmatched
a2$n2<-a2$Freq #fuzzy
a3$n3<-a3$Freq #exact
afinal<-cbind(a1,a2,a3)

afinal<-afinal[,c("Var1","n1","n2","n3")]
afinal$total<-round(afinal$n1+afinal$n2+afinal$n3,2)
afinal$matched<-round((afinal$n3+afinal$n2)/(afinal$total)*100,2)
afinal$unmatched<-round((afinal$n1/afinal$total)*100,2)
afinal$fuzzymatched<-round((afinal$n2/afinal$total)*100,2)
afinal$exactmatched<-round((afinal$n3/afinal$total)*100,2)

afinal$sample<-"Excluded"
afinal$sample[afinal$total>=2000 & afinal$exactmatched>=59 & afinal$total>=80]<-"Main Sample"

afinal <- afinal %>%                                        # Create ID by group
  group_by(sample) %>%
  dplyr::mutate(ID = row_number())

afinal<-afinal[,c("ID","Var1","total","unmatched","matched","exactmatched","fuzzymatched","sample")]
afinal$Var1<-as.character(afinal$Var1)

statefinal<-afinal$Var1[afinal$sample=="Main Sample"]
afinal$Var1<-str_to_title(afinal$Var1, locale = "en")
afinal <- afinal[order(afinal$ID), ]
afinal <- afinal[order(afinal$sample, decreasing=TRUE),]

colnames(afinal)<-c("Sno.","State","N Villages Connected","Umatched","Matched","Exact","Fuzzy","Sample")

stargazer::stargazer(afinal, summary = FALSE, rownames = FALSE, type="latex", header=FALSE, title='Matching results Roads-Census 2001 village dataset 2001-2018')
```
